package com.hexorld.core;

import java.util.ArrayList;

import com.hexorld.layer.Layer;

import android.content.Context;
import android.graphics.Canvas;

public class LayerManager {
	private ArrayList<Layer> mLayers = null;
	
	public LayerManager(){
		mLayers = new ArrayList<Layer>();
	}
	
	public void addLayer(int index, Layer layer){
		if(mLayers != null)
			mLayers.add(index, layer);
	}
	
	public void removeLayer(Layer layer){
		if(mLayers != null && mLayers.indexOf(layer) != -1)
			mLayers.remove(layer);
	}
	
	public void removeLayer(int index){
		if(mLayers != null && index < mLayers.size())
			mLayers.remove(index);
	}
	
	/*
	 * remove layer by Z
	 */
	public void removeLayerById(int id){
		if(mLayers == null)
			return;
		int count = mLayers.size();
		for(int i = count-1; i >= 0; i--)
		{
			if(mLayers.get(i).Z == id)
			{
				mLayers.remove(i);
				break;
			}
		}
	}
	
	public void removeAll(){
		if(mLayers != null)
			mLayers.clear();
	}
	
	public void updateAll(Context context, Canvas canvas){
		if(mLayers == null || mLayers.size() == 0)
			return;
		int count = mLayers.size();
		for(int i = 0; i < count; i++)
		{
			mLayers.get(i).draw(context, canvas);
		}
	}
	
	public void updateLayerByIndex(Context context, Canvas canvas, int index){
		if(mLayers != null && index < mLayers.size())
			mLayers.get(index).draw(context, canvas);
	}

	public Layer getLayerAt(int index){
		if(mLayers != null && index < mLayers.size())
			return mLayers.get(index);
		else
			return null;
	}
}
